using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Text;

namespace CodePlex.XmlDataMapper.Helpers
{
   internal static class ConfigurationHelper
   {
      internal static string GetMappingXmlFilePath(string typeName)
      {
         string sectionName = "CodePlex.XmlDataMapper/XmlMappingFiles";
         NameValueCollection nameValueCollection = ConfigurationManager.GetSection(sectionName) as NameValueCollection;
         if (nameValueCollection != null && nameValueCollection.HasKeys())
            if (!string.IsNullOrEmpty(nameValueCollection.Get(typeName)))
               return nameValueCollection.Get(typeName);
         return string.Empty;
      }

      internal static string GetRootPath()
      {
         string sectionName = "CodePlex.XmlDataMapper/ApplicationInformation";
         string key = "RootPath";
         return GetSectionValue(sectionName, key);
      }

      internal static bool IsWebApplication()
      { 
         string sectionName = "CodePlex.XmlDataMapper/ApplicationInformation";
         string key = "IsWebApplication";
         string isWebApplicationText = GetSectionValue(sectionName, key);
         bool isWebApplication;
         return bool.TryParse(isWebApplicationText, out isWebApplication) ? isWebApplication  : true;
      }

      private static string GetSectionValue(string sectionName, string key)
      {
         Hashtable hashtable = ConfigurationManager.GetSection(sectionName) as Hashtable;
         if (hashtable != null && hashtable.ContainsKey(key))
            return hashtable[key].ToString();
         return string.Empty;
      }
   }
}